#include "ASLogic.h"
#include "..\tool\ASLog.h"
#include "ASAppImgInfo.h"
#include "res_str.h"
#include "ASIO.h"


static as_item_content as_items[AS_ITEMS_COUNT];
static ASUINT s_items_cate[ENUM_CATEGORY_COUNT];
static ASUINT8 s_items_tried[AS_ITEMS_COUNT];
static ASUINT8 s_items_favorite[AS_ITEMS_COUNT];
static ASUINT8 s_items_todo[AS_ITEMS_COUNT];
static ASUINT8 s_items_recent[AS_ITEMS_COUNT];
static ASUINT8 s_items_untried[AS_ITEMS_COUNT];
static as_category_title s_cate_title[ENUM_CATEGORY_COUNT];

static as_global_config s_global_config;

#define AS_ADD_ITEMS(t_cn, img_s, img_l,text_cn, tried)   do{\
        as_items[idx].title_cn = t_cn;\
        as_items[idx].img_small = img_s;\
        as_items[idx].img_large = img_l;\
        as_items[idx].txt_cn = text_cn;\
        as_items[idx].btried = tried;\
        idx++;\
    }while(0);

#define AS_ADD_CATEGORY_TITLE(t_cn) do {\
        s_cate_title[idx].title_cn = t_cn; \
        idx++;\
    }while(0);

void as_logic_set_items_info(ASINT idx, ASUINT16 btried)
{
    if (idx < 0 || idx >= 106)
        return;

    as_items[idx].btried = btried;
}

void as_logic_init_items(void)
{
    ASINT idx = 0;
    AS_ADD_ITEMS(STR_P1_TITLE_CN, IMG_ITEM_PS1, IMG_ITEM_P1, "p1_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P2_TITLE_CN, IMG_ITEM_PS2, IMG_ITEM_P2, "p2_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P3_TITLE_CN, IMG_ITEM_PS3, IMG_ITEM_P3, "p3_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P4_TITLE_CN, IMG_ITEM_PS4, IMG_ITEM_P4, "p4_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P5_TITLE_CN, IMG_ITEM_PS5, IMG_ITEM_P5, "p5_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P6_TITLE_CN, IMG_ITEM_PS6, IMG_ITEM_P6, "p6_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P7_TITLE_CN, IMG_ITEM_PS7, IMG_ITEM_P7, "p7_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P8_TITLE_CN, IMG_ITEM_PS8, IMG_ITEM_P8, "p8_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P9_TITLE_CN, IMG_ITEM_PS9, IMG_ITEM_P9, "p9_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P10_TITLE_CN, IMG_ITEM_PS10, IMG_ITEM_P10, "p10_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P11_TITLE_CN, IMG_ITEM_PS11, IMG_ITEM_P11, "p11_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P12_TITLE_CN, IMG_ITEM_PS12, IMG_ITEM_P12, "p12_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P13_TITLE_CN, IMG_ITEM_PS13, IMG_ITEM_P13, "p13_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P14_TITLE_CN, IMG_ITEM_PS14, IMG_ITEM_P14, "p14_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P15_TITLE_CN, IMG_ITEM_PS15, IMG_ITEM_P15, "p15_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P16_TITLE_CN, IMG_ITEM_PS16, IMG_ITEM_P16, "p16_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P17_TITLE_CN, IMG_ITEM_PS17, IMG_ITEM_P17, "p17_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P18_TITLE_CN, IMG_ITEM_PS18, IMG_ITEM_P18, "p18_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P19_TITLE_CN, IMG_ITEM_PS19, IMG_ITEM_P19, "p19_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P20_TITLE_CN, IMG_ITEM_PS20, IMG_ITEM_P20, "p20_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P21_TITLE_CN, IMG_ITEM_PS21, IMG_ITEM_P21, "p21_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P22_TITLE_CN, IMG_ITEM_PS22, IMG_ITEM_P22, "p22_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P23_TITLE_CN, IMG_ITEM_PS23, IMG_ITEM_P23, "p23_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P24_TITLE_CN, IMG_ITEM_PS24, IMG_ITEM_P24, "p24_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P25_TITLE_CN, IMG_ITEM_PS25, IMG_ITEM_P25, "p25_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P26_TITLE_CN, IMG_ITEM_PS26, IMG_ITEM_P26, "p26_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P27_TITLE_CN, IMG_ITEM_PS27, IMG_ITEM_P27, "p27_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P28_TITLE_CN, IMG_ITEM_PS28, IMG_ITEM_P28, "p28_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P29_TITLE_CN, IMG_ITEM_PS29, IMG_ITEM_P29, "p29_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P30_TITLE_CN, IMG_ITEM_PS30, IMG_ITEM_P30, "p30_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P31_TITLE_CN, IMG_ITEM_PS31, IMG_ITEM_P31, "p31_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P32_TITLE_CN, IMG_ITEM_PS32, IMG_ITEM_P32, "p32_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P33_TITLE_CN, IMG_ITEM_PS33, IMG_ITEM_P33, "p33_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P34_TITLE_CN, IMG_ITEM_PS34, IMG_ITEM_P34, "p34_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P35_TITLE_CN, IMG_ITEM_PS35, IMG_ITEM_P35, "p35_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P36_TITLE_CN, IMG_ITEM_PS36, IMG_ITEM_P36, "p36_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P37_TITLE_CN, IMG_ITEM_PS37, IMG_ITEM_P37, "p37_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P38_TITLE_CN, IMG_ITEM_PS38, IMG_ITEM_P38, "p38_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P39_TITLE_CN, IMG_ITEM_PS39, IMG_ITEM_P39, "p39_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P40_TITLE_CN, IMG_ITEM_PS40, IMG_ITEM_P40, "p40_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P41_TITLE_CN, IMG_ITEM_PS41, IMG_ITEM_P41, "p41_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P42_TITLE_CN, IMG_ITEM_PS42, IMG_ITEM_P42, "p42_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P43_TITLE_CN, IMG_ITEM_PS43, IMG_ITEM_P43, "p43_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P44_TITLE_CN, IMG_ITEM_PS44, IMG_ITEM_P44, "p44_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P45_TITLE_CN, IMG_ITEM_PS45, IMG_ITEM_P45, "p45_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P46_TITLE_CN, IMG_ITEM_PS46, IMG_ITEM_P46, "p46_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P47_TITLE_CN, IMG_ITEM_PS47, IMG_ITEM_P47, "p47_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P48_TITLE_CN, IMG_ITEM_PS48, IMG_ITEM_P48, "p48_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P49_TITLE_CN, IMG_ITEM_PS49, IMG_ITEM_P49, "p49_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P50_TITLE_CN, IMG_ITEM_PS50, IMG_ITEM_P50, "p50_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P51_TITLE_CN, IMG_ITEM_PS51, IMG_ITEM_P51, "p51_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P52_TITLE_CN, IMG_ITEM_PS52, IMG_ITEM_P52, "p52_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P53_TITLE_CN, IMG_ITEM_PS53, IMG_ITEM_P53, "p53_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P54_TITLE_CN, IMG_ITEM_PS54, IMG_ITEM_P54, "p54_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P55_TITLE_CN, IMG_ITEM_PS55, IMG_ITEM_P55, "p55_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P56_TITLE_CN, IMG_ITEM_PS56, IMG_ITEM_P56, "p56_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P57_TITLE_CN, IMG_ITEM_PS57, IMG_ITEM_P57, "p57_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P58_TITLE_CN, IMG_ITEM_PS58, IMG_ITEM_P58, "p58_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P59_TITLE_CN, IMG_ITEM_PS59, IMG_ITEM_P59, "p59_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P60_TITLE_CN, IMG_ITEM_PS60, IMG_ITEM_P60, "p60_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P61_TITLE_CN, IMG_ITEM_PS61, IMG_ITEM_P61, "p61_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P62_TITLE_CN, IMG_ITEM_PS62, IMG_ITEM_P62, "p62_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P63_TITLE_CN, IMG_ITEM_PS63, IMG_ITEM_P63, "p63_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P64_TITLE_CN, IMG_ITEM_PS64, IMG_ITEM_P64, "p64_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P65_TITLE_CN, IMG_ITEM_PS65, IMG_ITEM_P65, "p65_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P66_TITLE_CN, IMG_ITEM_PS66, IMG_ITEM_P66, "p66_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P67_TITLE_CN, IMG_ITEM_PS67, IMG_ITEM_P67, "p67_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P68_TITLE_CN, IMG_ITEM_PS68, IMG_ITEM_P68, "p68_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P69_TITLE_CN, IMG_ITEM_PS69, IMG_ITEM_P69, "p69_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P70_TITLE_CN, IMG_ITEM_PS70, IMG_ITEM_P70, "p70_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P71_TITLE_CN, IMG_ITEM_PS71, IMG_ITEM_P71, "p71_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P72_TITLE_CN, IMG_ITEM_PS72, IMG_ITEM_P72, "p72_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P73_TITLE_CN, IMG_ITEM_PS73, IMG_ITEM_P73, "p73_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P74_TITLE_CN, IMG_ITEM_PS74, IMG_ITEM_P74, "p74_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P75_TITLE_CN, IMG_ITEM_PS75, IMG_ITEM_P75, "p75_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P76_TITLE_CN, IMG_ITEM_PS76, IMG_ITEM_P76, "p76_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P77_TITLE_CN, IMG_ITEM_PS77, IMG_ITEM_P77, "p77_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P78_TITLE_CN, IMG_ITEM_PS78, IMG_ITEM_P78, "p78_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P79_TITLE_CN, IMG_ITEM_PS79, IMG_ITEM_P79, "p79_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P80_TITLE_CN, IMG_ITEM_PS80, IMG_ITEM_P80, "p80_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P81_TITLE_CN, IMG_ITEM_PS81, IMG_ITEM_P81, "p81_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P82_TITLE_CN, IMG_ITEM_PS82, IMG_ITEM_P82, "p82_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P83_TITLE_CN, IMG_ITEM_PS83, IMG_ITEM_P83, "p83_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P84_TITLE_CN, IMG_ITEM_PS84, IMG_ITEM_P84, "p84_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P85_TITLE_CN, IMG_ITEM_PS85, IMG_ITEM_P85, "p85_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P86_TITLE_CN, IMG_ITEM_PS86, IMG_ITEM_P86, "p86_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P87_TITLE_CN, IMG_ITEM_PS87, IMG_ITEM_P87, "p87_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P88_TITLE_CN, IMG_ITEM_PS88, IMG_ITEM_P88, "p88_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P89_TITLE_CN, IMG_ITEM_PS89, IMG_ITEM_P89, "p89_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P90_TITLE_CN, IMG_ITEM_PS90, IMG_ITEM_P90, "p90_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P91_TITLE_CN, IMG_ITEM_PS91, IMG_ITEM_P91, "p91_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P92_TITLE_CN, IMG_ITEM_PS92, IMG_ITEM_P92, "p92_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P93_TITLE_CN, IMG_ITEM_PS93, IMG_ITEM_P93, "p93_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P94_TITLE_CN, IMG_ITEM_PS94, IMG_ITEM_P94, "p94_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P95_TITLE_CN, IMG_ITEM_PS95, IMG_ITEM_P95, "p95_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P96_TITLE_CN, IMG_ITEM_PS96, IMG_ITEM_P96, "p96_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P97_TITLE_CN, IMG_ITEM_PS97, IMG_ITEM_P97, "p97_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P98_TITLE_CN, IMG_ITEM_PS98, IMG_ITEM_P98, "p98_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P99_TITLE_CN, IMG_ITEM_PS99, IMG_ITEM_P99, "p99_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P100_TITLE_CN, IMG_ITEM_PS100, IMG_ITEM_P100, "p100_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P101_TITLE_CN, IMG_ITEM_PS101, IMG_ITEM_P101, "p101_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P102_TITLE_CN, IMG_ITEM_PS102, IMG_ITEM_P102, "p102_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P103_TITLE_CN, IMG_ITEM_PS103, IMG_ITEM_P103, "p103_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P104_TITLE_CN, IMG_ITEM_PS104, IMG_ITEM_P104, "p104_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P105_TITLE_CN, IMG_ITEM_PS105, IMG_ITEM_P105, "p105_cn.txt", ASFALSE);
    AS_ADD_ITEMS(STR_P106_TITLE_CN, IMG_ITEM_PS106, IMG_ITEM_P106, "p106_cn.txt", ASFALSE);
}

void as_logic_init_category_title(void)
{
    ASINT idx = 0;
    AS_ADD_CATEGORY_TITLE(STR_CATE_SIXTY_CN);
    AS_ADD_CATEGORY_TITLE(STR_CATE_BUTTERFLY_CN);
    AS_ADD_CATEGORY_TITLE(STR_CATE_COWGIRL_CN);
    AS_ADD_CATEGORY_TITLE(STR_CATE_CUNNI_CN);
    AS_ADD_CATEGORY_TITLE(STR_CATE_DOGGY_CN);
    AS_ADD_CATEGORY_TITLE(STR_CATE_EXOTIC_CN);
    AS_ADD_CATEGORY_TITLE(STR_CATE_F2F_CN);
    AS_ADD_CATEGORY_TITLE(STR_CATE_FELL_CN);
    AS_ADD_CATEGORY_TITLE(STR_CATE_SPOON_CN);
}

as_category_title* as_logic_get_category_title(ASINT categoryid)
{
    return s_cate_title + categoryid;
}


void as_logic_init_category(void)
{
    s_items_cate[ENUM_CATEGORY_SIXTY] = AS_CATEGORY_SIXTY_COUNT;
    s_items_cate[ENUM_CATEGORY_BUTTERFLY] = AS_CATEGORY_BUTTERFLY_COUNT;
    s_items_cate[ENUM_CATEGORY_COWGIRL] = AS_CATEGORY_COWGIRL_COUNT;
    s_items_cate[ENUM_CATEGORY_CUNNI] = AS_CATEGORY_CUNNI_COUNT;
    s_items_cate[ENUM_CATEGORY_DOGGY] = AS_CATEGORY_DOGGY_COUNT;
    s_items_cate[ENUM_CATEGORY_EXOTIC] = AS_CATEGORY_EXOTIC_COUNT;
    s_items_cate[ENUM_CATEGORY_FACETOFACE] = AS_CATEGORY_FACETOFACE_COUNT;
    s_items_cate[ENUM_CATEGORY_FELL] = AS_CATEGORY_FELL_COUNT;
    s_items_cate[ENUM_CATEGORY_SPOON] = AS_CATEGORY_SPOON_COUNT;
}

ASINT as_logic_translate_idx(ASINT idx, ASINT cate_id)
{
    ASINT i, new_idx = idx;

    if (cate_id >= ENUM_CATEGORY_SIXTY && cate_id < ENUM_CATEGORY_COUNT)
    {
        for(i = 0; i < cate_id; i++)
            new_idx += s_items_cate[i];
    }

    return new_idx;
}

as_item_content* as_logic_get_item(ASINT idx)
{
    if (idx < 0 || idx >= 106)
    {
        as_log("as_scr_list_get_item idx error idx:%d", idx);
        return ASNULL;
    }

    return &as_items[idx];
}

void as_logic_get_category(ASINT idx, ASINT* idx_begin, ASINT* idx_end)
{
    ASINT i, start = 0;

    for (i = ENUM_CATEGORY_SIXTY; i < ENUM_CATEGORY_COUNT; i++)
    {
        if (idx == i)
        {
            *idx_begin = start;
            *idx_end = start + s_items_cate[i];
            break;
        }
        else
        {
            start += s_items_cate[i];
        }
    }
}
void as_logic_init_tried(void)
{
    ASINT i = 0;

    for (i = 0; i < AS_ITEMS_COUNT; i++)
    {
        if (s_items_tried[i] != AS_UNVALID_ITEM_ID)
        {
            as_logic_set_items_info(s_items_tried[i], ASTRUE);
        }
    }
}
void as_logic_init_item_record(void)
{
    if (mrc_getLen(IKAMA_RECORD_FILE) <= 0)
    {
        as_memset(s_items_favorite, AS_UNVALID_ITEM_ID, sizeof(s_items_favorite));
        as_memset(s_items_tried, AS_UNVALID_ITEM_ID, sizeof(s_items_tried));
        as_memset(s_items_todo, AS_UNVALID_ITEM_ID, sizeof(s_items_todo));
        as_memset(s_items_recent, AS_UNVALID_ITEM_ID, sizeof(s_items_recent));
        as_io_write_record(SI_FAVORITE, (void*)s_items_favorite, sizeof(s_items_favorite));
        as_io_write_record(SI_TRIED, (void*)s_items_tried, sizeof(s_items_tried));
        as_io_write_record(SI_TODO, (void*)s_items_todo, sizeof(s_items_todo));
        as_io_write_record(SI_RECENT, (void*)s_items_recent, sizeof(s_items_recent));
    }
    else
    {
        as_io_read_record(SI_FAVORITE, (void*)s_items_favorite, sizeof(s_items_favorite));
        as_io_read_record(SI_TRIED, (void*)s_items_tried, sizeof(s_items_tried));
        as_io_read_record(SI_TODO, (void*)s_items_todo, sizeof(s_items_todo));
        as_io_read_record(SI_RECENT, (void*)s_items_recent, sizeof(s_items_recent));
    }

    as_logic_init_tried();
}

void as_logic_add_recent(ASINT itemid)
{
    ASINT i;

    if (itemid < 0 || itemid >= 106)
        return;

    for(i = AS_ITEMS_COUNT - 1; i >= 0; i--)
    {
        if (s_items_recent[i] == itemid)
            return;
    }

    for(i = AS_ITEMS_COUNT - 1; i >= 0; i--)
    {
        if (s_items_recent[i] == AS_UNVALID_ITEM_ID)
        {
            s_items_recent[i] = itemid;
            break;
        }
    }

    as_io_write_record(SI_RECENT, s_items_recent, sizeof(s_items_recent));
}

void as_logic_add_favorite(ASINT itemid)
{
    ASINT i;

    if (itemid < 0 && itemid >= 106)
        return;

    for (i = AS_ITEMS_COUNT - 1; i >= 0; i--)
    {
        if (s_items_favorite[i] == itemid)
            return;
    }

    for (i = AS_ITEMS_COUNT - 1; i >= 0; i--)
    {
        if(s_items_favorite[i] == AS_UNVALID_ITEM_ID)
        {
            s_items_favorite[i] = itemid;
            break;
        }
    }

    as_io_write_record(SI_FAVORITE, s_items_favorite, sizeof(s_items_favorite));
}

void as_logic_del_favorite(ASINT itemid)
{
    ASINT i = AS_ITEMS_COUNT - 1;
    ASINT j;

    if (itemid < 0 && itemid >= 106)
        return;

    for (; i >= 0; i--)
    {
        if (itemid == s_items_favorite[i])
        {
            s_items_favorite[i] = AS_UNVALID_ITEM_ID;
            break;
        }
    }

    for (j = i; j >= 0; j--)
    {
        if (s_items_favorite[j - 1] != AS_UNVALID_ITEM_ID)
        {
            s_items_favorite[j] = s_items_favorite[j - 1];
            s_items_favorite[j - 1] = AS_UNVALID_ITEM_ID;
        }
        else
            break;
    }

    as_io_write_record(SI_FAVORITE, s_items_favorite, sizeof(s_items_favorite));
}

void as_logic_add_tried(ASINT itemid)
{
    ASINT i;

    if (itemid < 0 && itemid >= 106)
        return;

    for (i = AS_ITEMS_COUNT - 1; i >= 0; i--)
    {
        if (s_items_tried[i] == itemid)
            return;
    }

    for (i = AS_ITEMS_COUNT - 1; i >= 0; i--)
    {
        if(s_items_tried[i] == AS_UNVALID_ITEM_ID)
        {
            s_items_tried[i] = itemid;
            break;
        }
    }

    as_items[itemid].btried = ASTRUE;
    as_io_write_record(SI_TRIED, s_items_tried, sizeof(s_items_tried));
}

void as_logic_del_tried(ASINT itemid)
{
    ASINT i = AS_ITEMS_COUNT - 1;
    ASINT j;

    if (itemid < 0 && itemid >= 106)
        return;

    for (; i >= 0; i--)
    {
        if (itemid == s_items_tried[i])
        {
            s_items_tried[i] = AS_UNVALID_ITEM_ID;
            break;
        }
    }

    for (j = i; j >= 0; j--)
    {
        if (s_items_tried[j - 1] != AS_UNVALID_ITEM_ID)
        {
            s_items_tried[j] = s_items_tried[j - 1];
            s_items_tried[j - 1] = AS_UNVALID_ITEM_ID;
        }
        else
            break;
    }

    as_items[itemid].btried = ASFALSE;
    as_io_write_record(SI_TRIED, s_items_tried, sizeof(s_items_tried));
}
void as_logic_add_todo(ASINT itemid)
{
    ASINT i;

    if (itemid < 0 && itemid >= 106)
        return;

    for (i = AS_ITEMS_COUNT - 1; i >= 0; i--)
    {
        if (s_items_todo[i] == itemid)
            return;
    }

    for (i = AS_ITEMS_COUNT - 1; i >= 0; i--)
    {
        if(s_items_todo[i] == AS_UNVALID_ITEM_ID)
        {
            s_items_todo[i] = itemid;
            break;
        }
    }

    as_io_write_record(SI_TODO, s_items_todo, sizeof(s_items_todo));
}
void as_logic_del_todo(ASINT itemid)
{
    ASINT i = AS_ITEMS_COUNT - 1;
    ASINT j;

    if (itemid < 0 && itemid >= 106)
        return;

    for (; i >= 0; i--)
    {
        if (itemid == s_items_todo[i])
        {
            s_items_todo[i] = AS_UNVALID_ITEM_ID;
            break;
        }
    }

    for (j = i; j >= 0; j--)
    {
        if (s_items_todo[j - 1] != AS_UNVALID_ITEM_ID)
        {
            s_items_todo[j] = s_items_todo[j - 1];
            s_items_todo[j - 1] = AS_UNVALID_ITEM_ID;
        }
        else
            break;
    }

    as_io_write_record(SI_TODO, s_items_todo, sizeof(s_items_todo));
}

static void as_logic_get_untried(void)
{
    ASINT i = 0, j = 0;

    for (; i < AS_ITEMS_COUNT; i++)
    {
        if(s_items_tried[i] != i)
        {
            s_items_untried[j] = i;
            j++;
        }
    }
}

ASUINT8* as_logic_get_record(ASINT categoryid)
{
    if (categoryid == ENUM_RECORD_FAVORITE)
    {
        return s_items_favorite;
    }
    else if (categoryid == ENUM_RECORD_TRIED)
    {
        return s_items_tried;
    }
    else if (categoryid == ENUM_RECORD_RECENT)
    {
        return s_items_recent;
    }
    else if (categoryid == ENUM_RECORD_UNTRIED)
    {
        as_logic_get_untried();
        return s_items_untried;
    }
    else if (categoryid == ENUM_RECORD_TODO)
    {
        return s_items_todo;
    }
    else
        return ASNULL;
}

ASINT as_logic_get_favorite_count(void)
{
    ASINT count = 0, i;

    for (i = 0; i < AS_ITEMS_COUNT; i++)
    {
        if (s_items_favorite[i] != AS_UNVALID_ITEM_ID)
            count++;
    }

    return count;
}

ASINT as_logic_get_tried_count()
{
    ASINT count = 0, i;

    for (i = 0; i < AS_ITEMS_COUNT; i++)
    {
        if (s_items_tried[i] != AS_UNVALID_ITEM_ID)
            count++;
    }

    return count;
}
ASINT as_logic_get_untried_count(void)
{
    return AS_ITEMS_COUNT - as_logic_get_tried_count();
}

ASINT as_logic_get_todo_count(void)
{
    ASINT count = 0, i;

    for (i = 0; i < AS_ITEMS_COUNT; i++)
    {
        if (s_items_todo[i] != AS_UNVALID_ITEM_ID)
            count++;
    }

    return count;
}

ASINT as_logic_get_recent_count(void)
{
    ASINT count = 0, i;

    for (i = 0; i < AS_ITEMS_COUNT; i++)
    {
        if (s_items_recent[i] != AS_UNVALID_ITEM_ID)
            count++;
    }

    return count;
}

ASINT as_logic_get_record_count(ASINT categoryid)
{
    switch(categoryid)
    {
        case ENUM_RECORD_TODO:
            return as_logic_get_todo_count();

        case ENUM_RECORD_FAVORITE:
            return as_logic_get_favorite_count();

        case ENUM_RECORD_TRIED:
            return as_logic_get_tried_count();

        case ENUM_RECORD_UNTRIED:
            return as_logic_get_untried_count();

        case ENUM_RECORD_RECENT:
            return as_logic_get_recent_count();

        case ENUM_RECORD_ALL:
            return AS_ITEMS_COUNT;

        default:
            return 0;
    }
}

as_global_config* as_get_global_config(void)
{
    return &s_global_config;
}

void as_init_global_config(void)
{
    s_global_config.music_switch = ON;
}

ASINT as_logic_get_category_tried_count(ASINT categoryid)
{
    ASINT idx_begin = 0, idx_end = 0, i, count = 0;

    if (categoryid == ENUM_RECORD_ALL)
    {
        idx_begin = 0;
        idx_end = AS_ITEMS_COUNT;
    }
    else
        as_logic_get_category(categoryid, &idx_begin, &idx_end);

    for (i = idx_begin; i < idx_end; i++)
    {
        if (as_items[i].btried == ASTRUE)
            count++;
    }

    return count;
}

ASINT as_logic_get_percent(ASINT categoryid)
{
    ASINT count, percent;
    count = as_logic_get_category_tried_count(categoryid);

    if (categoryid == ENUM_RECORD_ALL)
        percent = count * 100 / AS_ITEMS_COUNT;
    else
        percent = count * 100 / s_items_cate[categoryid];

    return percent;
}